﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;

namespace Damar
{
    static class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("pt-PT");
            if (args.Length == 2)
                Import(Convert.ToDateTime(args[0]), Convert.ToDateTime(args[1]));
            else
                Run();
        }

        private static void Run()
        {
            var controller = new Controller();
            controller.SetLogFile();
            if (!controller.IsLicenseValid())
                return;
            if (!controller.ServerConnectionOpened())
                return;
            if (!controller.OpenPrimaveraCompany())
                return;
            var mainForm = new MainRibbonForm { Controller = controller };
            Application.Run(mainForm);
        }

        private static void Import(DateTime start, DateTime end)
        {
            var controller = new Controller { Start = start, End = end };
            controller.SetLogFile();
            if (!controller.CanImport())
                return;
            if (!controller.OpenPrimaveraCompany())
                return;
            controller.Import();
            controller.ClosePrimaveraCompany();
        }
    }
}
